package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemu;
import cn.stylefeng.guns.sys.modular.system.factory.LayuiTreeFactory;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxHuopinParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxKemuParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxHuopinResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxKemuResult;
import cn.stylefeng.guns.sys.modular.system.service.ZxHuopinService;
import cn.stylefeng.guns.sys.modular.system.service.ZxKemuService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 装修科目表控制器
 *
 * @author gyz
 * @Date 2020-11-02 13:43:19
 */
@Controller
@RequestMapping("/zxKemu")
public class ZxKemuController extends BaseController {

    private String PREFIX = "/modular/system/zxKemu";

    @Autowired
    private ZxKemuService zxKemuService;
    @Autowired
    private ZxHuopinService zxHuopinService;

    /**
     * 跳转到主页面
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/zxKemu.html";
    }

    /**
     * 新增页面
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxKemu_add.html";
    }

    /**
     * 编辑页面
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/zxKemu_edit.html";
    }

    /**
     * 新增接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxKemuParam zxKemuParam) {
        ZxKemuParam param = new ZxKemuParam();
        param.setPid(zxKemuParam.getPid());
        param.setName(zxKemuParam.getName());
        ZxKemuResult bySpec = this.zxKemuService.findBySpec(param);
        if(ToolUtil.isNotEmpty(bySpec)){
            return ResponseData.error(1,"科目名称已存在");
        }
        ZxKemu km = this.zxKemuService.getById(zxKemuParam.getPid());
        if(km!= null && Integer.valueOf(km.getLevel())>=3){
            return ResponseData.error(1,"不允许添加四级科目");
        }
        this.zxKemuService.add(zxKemuParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxKemuParam zxKemuParam) {
        ZxKemuParam param = new ZxKemuParam();
        param.setPid(zxKemuParam.getPid());
        param.setName(zxKemuParam.getName());
        ZxKemuResult bySpec = this.zxKemuService.findBySpec(param);
        if(ToolUtil.isNotEmpty(bySpec) && bySpec.getBtyZxKemuId().longValue()!=zxKemuParam.getBtyZxKemuId().longValue()){
            return ResponseData.error(1,"科目名称已存在");
        }
        ZxKemu km = this.zxKemuService.getById(zxKemuParam.getPid());
        if(km!= null && Integer.valueOf(km.getLevel())>=3){
            return ResponseData.error(1,"不允许添加四级科目");
        }
        this.zxKemuService.update(zxKemuParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxKemuParam zxKemuParam) {
        //查询是否使用中,禁止删除
        ZxKemu km = zxKemuService.getById(zxKemuParam.getBtyZxKemuId());
        if(Integer.valueOf(km.getLevel())==1){
            ZxKemuParam p=new ZxKemuParam();
            p.setPid(zxKemuParam.getBtyZxKemuId());
            List<ZxKemuResult> childs = this.zxKemuService.findListBySpec(p);
            if(ToolUtil.isNotEmpty(childs)){
                return ResponseData.error(1,"此科目下有二级科目不允许删除!");
            }
        }else if(Integer.valueOf(km.getLevel())==2){
            ZxHuopinParam hp = new ZxHuopinParam();
            hp.setBtyZxKemuId(zxKemuParam.getBtyZxKemuId());
            List<ZxHuopinResult> hplist = zxHuopinService.findListBySpec(hp);
            if(ToolUtil.isNotEmpty(hplist)){
                return ResponseData.error(1,"科目下有货品信息不允许删除!");
            }
        }
        this.zxKemuService.delete(zxKemuParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxKemuParam zxKemuParam) {
        //父级科目名称
        ZxKemu detail = this.zxKemuService.getDetailById(zxKemuParam.getBtyZxKemuId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxKemuParam zxKemuParam) {
        return this.zxKemuService.findPageBySpec(zxKemuParam);
    }

    @RequestMapping("/layuiTree")
    @ResponseBody
    public List<LayuiTreeNode> layuiTree(){
        List<LayuiTreeNode> list = this.zxKemuService.layuiTree();
        list.add(LayuiTreeFactory.createRoot());
        DefaultTreeBuildFactory<LayuiTreeNode> treeBuildFactory = new DefaultTreeBuildFactory<>();
        treeBuildFactory.setRootParentId("-1");

        return treeBuildFactory.doTreeBuild(list);
    }

    /**
     * 获取kemu的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2019-8-27 15:24
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.zxKemuService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

}


